package org.rslai.autotest.core.page.impl;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;
import org.rslai.autotest.core.page.AbstractHelper;
import org.rslai.autotest.core.page.AutoIt;
import org.rslai.autotest.core.page.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * UploadFile处理方法
 * @author rslai
 */
public class UploadFileImpl extends AbstractHelper implements UploadFile {
	@Autowired
	AutoIt autoIt;

	@Autowired
	org.rslai.autotest.core.page.Robot robot;

	@Override
	public void setUploadFileNameForInput(By by, String fullFileName) {
		this.driver().findElement(by).sendKeys(fullFileName);
	}

	@Override
	@Deprecated
	public void inputUploadFileNameForInputUseRobot(By by, String fullFileName) {
		try {
			// 打开上传文件对话框
			Actions action = new Actions(this.driver());
			action.doubleClick(this.driver().findElement(by));
			action.perform();
			Robot robot;
			robot = new Robot();
			robot.keyPress(KeyEvent.VK_ENTER);

			// 将制定文件名存到剪贴板中
			StringSelection stringSelection = new StringSelection(fullFileName);		    
	        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

	        super.sleep(3);

	        // 利用键盘事件Ctrl + V,粘贴文件路径
			robot.keyPress(KeyEvent.VK_CONTROL);
			robot.keyPress(KeyEvent.VK_V);
			robot.keyRelease(KeyEvent.VK_V);
			robot.keyRelease(KeyEvent.VK_CONTROL);
			
			// 再按回车关掉上传文件对话框
			robot.keyPress(KeyEvent.VK_ENTER);
			robot.keyRelease(KeyEvent.VK_ENTER);
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	@Override
	@Deprecated
	public void inputUploadFileNameForInputUseAutoit(By by, String fullFileName) throws IOException, InterruptedException {
		try{
			// 打开上传文件对话框
			Actions action = new Actions(this.driver());
			action.doubleClick(this.driver().findElement(by));
			action.perform();
			Robot robot;
			robot = new Robot();
			robot.keyPress(KeyEvent.VK_ENTER);
		} catch (AWTException e) {
			e.printStackTrace();
		}

		this.autoIt.inputUpLoadFileName(fullFileName);
	}

	@Override
	public void inputUploadFileNameForFlashUseRobot(By by, String fullFileName) {
		try {
			// 打开上传文件对话框
			this.driver().findElement(by).click();
			
			// 将制定文件名存到剪贴板中
			StringSelection stringSelection = new StringSelection(fullFileName);		    
	        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

	        super.sleep(3);

			Robot robot;
			robot = new Robot();
			// 利用键盘事件Ctrl + V,粘贴文件路径
			robot.keyPress(KeyEvent.VK_CONTROL);
			robot.keyPress(KeyEvent.VK_V);
			robot.keyRelease(KeyEvent.VK_V);
			robot.keyRelease(KeyEvent.VK_CONTROL);
			
			
			// 再按回车关掉上传文件对话框
			robot.keyPress(KeyEvent.VK_ENTER);
			robot.keyRelease(KeyEvent.VK_ENTER);

			super.sleep(2);
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void inputUploadFileNameForFlashUseAutoit(By by, String fullFileName) throws IOException, InterruptedException {
		this.driver().findElement(by).click();
		this.autoIt.inputUpLoadFileName(fullFileName);
	}

	@Override
	public void inputUploadFileNameForFlashUseAutoitAndRobot(By by, String fullFileName) throws AWTException, IOException, InterruptedException {
		this.robot.click(by);
		this.autoIt.inputUpLoadFileName(fullFileName);
	}

}
